﻿using JNPF.Basics.Models;
using JNPF.Basics.Portal;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 
    /// </summary>
    public class PortalBll
    {
        private PortalIService service = new PortalService();
        private UserIService userService = new UserService();
        private AuthorizeIService authorizeService = new AuthorizeService();
        private DictionaryDataIService dictionaryDataService = new DictionaryDataService();

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns>返回列表</returns>
        public async Task<PageResult<PortalEntity>> GetList(PageInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns>返回列表</returns>
        public async Task<List<PortalEntity>> GetList()
        {
            try
            {
                return await service.GetList();
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        public async Task<List<PortalSelectOutput>> GetSelector(string type)
        {
            var list = (await this.GetList()).FindAll(x => x.EnabledMark == 1);
            UserInfo userInfo = UserProvider.Instance.Get();
            if (type == "1" && userInfo.IsAdministrator != true)
            {
                UserEntity model = (await userService.GetList()).FindAll(u => u.Id == userInfo.UserId).FirstOrDefault();
                if (model != null)
                {
                    List<string> roleIds = new List<string>();
                    List<string> itemIds = new List<string>();
                    List<PortalEntity> newPortalList = new List<PortalEntity>();
                    roleIds = model.RoleId.Split(',').ToList();
                    if (roleIds.Count > 0)
                    {
                        foreach (var item in roleIds)
                        {
                            List<AuthorizeEntity> authorizeEntityList = (await authorizeService.GetListByObjectId(item)).FindAll(p => p.ItemType == "portal");
                            if (authorizeEntityList.Count > 0)
                            {
                                authorizeEntityList.ForEach(a =>
                                {
                                    itemIds.Add(a.ItemId);
                                });
                            }
                        }
                    }
                    itemIds.ForEach(i =>
                    {
                        var entity = list.FindAll(f => f.Id == i).FirstOrDefault();
                        if (entity != null)
                        {
                            newPortalList.Add(entity);
                        }
                    });
                    list = newPortalList;
                }
                else
                {
                    list = new List<PortalEntity>();
                }
            }
            List<PortalSelectOutput> modelList = list.MapToList<PortalSelectOutput>();
            foreach (var item in list)
            {
                DictionaryDataEntity dictionaryDataEntity = await dictionaryDataService.GetInfo(item.Category);
                if (dictionaryDataEntity != null)
                {
                    PortalSelectOutput model = new PortalSelectOutput();
                    model.id = dictionaryDataEntity.Id;
                    model.fullName = dictionaryDataEntity.FullName;
                    model.parentId = "0";
                    if (!modelList.Exists(l => l.id == model.id))
                    {
                        modelList.Add(model);
                    }
                }
            }
            //处理modelList 父节点
            foreach (var item in modelList)
            {
                if (item.category != null)
                {
                    item.parentId = item.category;
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获取门户信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<PortalEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 创建新功能信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Create(PortalEntity entity)
        {
            try
            {
                entity.Id = CommonHelper.Guid;
                entity.SortCode = 0;
                entity.CreatorTime = DateTime.Now;
                entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                return await service.Create(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        public async Task<int> Update(string id, PortalEntity entity)
        {
            try
            {
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Delete(PortalEntity entity)
        {
            try
            {
                return await service.Delete(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<PortalEntity> GetInfoAuth(string id)
        {
            PortalEntity entity = new PortalEntity();
            UserInfo userInfo = UserProvider.Instance.Get();
            if (userInfo != null && userInfo.RoleIds != null)
            {
                var roleIds = userInfo.RoleIds.ToList<string>();
                foreach (var item in roleIds)
                {
                    List<AuthorizeEntity> authorizeEntityList = (await authorizeService.GetListByObjectId(item)).FindAll(p => p.ItemType == "portal");
                    var model = authorizeEntityList.Find(a => a.ItemId == id);
                    if (model != null)
                    {
                        entity = await service.GetInfo(id);
                    }
                }
            }
            if (userInfo.IsAdministrator == true)
            {
                entity = await service.GetInfo(id);
            }
            return entity;
        }
    }
}
